﻿/*
helferlein.com ( http://www.helferlein.com )
Michael Tobisch

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions 
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using helferlein.DNN.Modules.Form.Business;
using System.Data.SqlClient;
using System.Web;
using DotNetNuke.Security;
using helferlein.DNN.Modules.BabelFish.Business;
using System.Globalization;
using helferlein.DNN.Modules.BabelFish.UI;
using System.Text;

namespace helferlein.DNN.Modules.Form.UI
{
   partial class FormEdit : PortalModuleBase
   {
#region Private Properties
      private FormItemInfo formItem;
      private FormItemController controller = null;
      private BabelFishController babelFishController = null;
      private List<FormItemInfo> formItems;
      private int? formItemIndex;
      private PortalSecurity portalSecurity = null;

      protected FormItemInfo FormItem
      {
         get { return formItem; }
         set { formItem = value; }
      }

      protected FormItemController Controller
      {
         get
         {
            if (controller == null)
               controller = new FormItemController();
            return controller;
         }
      }

      protected BabelFishController BabelFishController
      {
         get
         {
            if (babelFishController == null)
               babelFishController = new BabelFishController();
            return babelFishController;
         }
      }

      protected List<FormItemInfo> FormItems
      {
         get
         {
            if (formItems == null)
               formItems = Controller.GetAll(this.FormItem.ModuleID);
            return formItems;
         }
      }

      protected bool HasSendToItem
      {
         get
         {
            bool hasSentToItem = false;
            foreach (FormItemInfo fii in this.FormItems)
            {
               if (fii.FormItemType == FormItemType.SendTo)
               {
                  hasSentToItem = true;
                  break;
               }
            }
            return hasSentToItem;
         }
      }

      protected bool HideSendCopy
      {
         get
         {
            bool hideSendCopy = false;

            if (!(Convert.ToBoolean(this.SubmitByEMail)))
               hideSendCopy = true;
            else
            {
               foreach (FormItemInfo fii in this.FormItems)
               {
                  if (fii.FormItemType == FormItemType.SendCopy)
                  {
                     hideSendCopy = true;
                     break;
                  }
               }
               if (!(hideSendCopy))
                  hideSendCopy = ((this.SenderAddress == "[ADMIN]") || (this.SenderAddress == "[HOST]") || (this.SenderAddress.StartsWith("[USERID]")));
            }
            return hideSendCopy;
         }
      }

      protected int FormItemIndex
      {
         get
         {
            if (formItemIndex == null)
            {
               for (int i = 0; i < this.FormItems.Count; i++)
               {
                  if (this.FormItems[i].FormItemID == this.FormItem.FormItemID)
                  {
                     formItemIndex = i;
                     break;
                  }
               }
            }
            return formItemIndex.Value;
         }
      }

      protected PortalSecurity PortalSecurity
      {
         get
         {
            if (portalSecurity == null)
               portalSecurity = new PortalSecurity();
            return portalSecurity;
         }
      }

      protected Dictionary<string, string> Aquarium
      {
         get
         {
            object o = ViewState["Aquarium"];
            if (o == null)
               ViewState["Aquarium"] = new Dictionary<string, string>();
            return (Dictionary<string, string>)ViewState["Aquarium"];
         }
         set { ViewState["Aquarium"] = value; }
      }

      protected string SharedResourceFile
      {
         get
         {
            return base.ControlPath + Localization.LocalResourceDirectory + "/" + Localization.LocalSharedResourceFile;
         }
      }
#endregion

#region Settings
      protected bool SubmitByEMail
      {
         get { return Convert.ToBoolean(base.Settings["SubmitByEMail"]); }
      }

      protected string SenderAddress
      {
         get
         {
            object o = base.Settings["SenderAddress"];
            if (o == null)
               return PortalSettings.Email;
            else
               return o.ToString();
         }
      }

      protected string AddToDropDownListItem
      {
         get { return (Convert.ToString(base.Settings["AddToDropDownList"])); }
      }

      protected string GroupOutputByItem
      {
         get { return (Convert.ToString(base.Settings["GroupOutputBy"])); }
      }

      protected String RenderAsHeadlineItem
      {
         get { return (Convert.ToString(base.Settings["RenderAsHeadline"])); }
      }

      protected string FormHeaderTemplate
      {
         get
         {
            object o = base.Settings["FormHeaderTemplate"];
            if (o == null)
               return Base.DEFAULT_FORM_HEADER_TEMPLATE;
            else
            {
               try { return Server.HtmlDecode(Convert.ToString(o)); }
               catch { return Base.DEFAULT_FORM_HEADER_TEMPLATE; }
            }
         }
      }

      protected string FormItemTemplate
      {
         get
         {
            object o = base.Settings["FormItemTemplate"];
            if (o == null)
               return Base.DEFAULT_FORM_ITEM_TEMPLATE;
            else
            {
               try { return Server.HtmlDecode(Convert.ToString(o)); }
               catch { return Base.DEFAULT_FORM_ITEM_TEMPLATE; }
            }
         }
      }

      protected string LabelTemplate
      {
         get
         {
            object o = base.Settings["LabelTemplate"];
            if (o == null)
               return Base.DEFAULT_FORM_LABEL_TEMPLATE;
            else
            {
               try { return Server.HtmlDecode(Convert.ToString(o)); }
               catch { return Base.DEFAULT_FORM_LABEL_TEMPLATE; }
            }
         }
      }

      protected string FormFooterTemplate
      {
         get
         {
            object o = base.Settings["FormFooterTemplate"];
            if (o == null)
               return Base.DEFAULT_FORM_FOOTER_TEMPLATE;
            else
            {
               try { return Server.HtmlDecode(Convert.ToString(o)); }
               catch { return Base.DEFAULT_FORM_FOOTER_TEMPLATE; }
            }
         }
      }
#endregion

#region Event Handlers
      protected void Page_Init(object sender, EventArgs e)
      {
         this.UpdateButton.Click += new EventHandler(this.UpdateButton_Click);
         this.CancelButton.Click += new EventHandler(this.CancelButton_Click);
         this.DeleteButton.Click += new EventHandler(this.DeleteButton_Click);
         this.MoveTop.Click += new EventHandler(this.MoveTop_Click);
         this.MoveUp.Click += new EventHandler(this.MoveUp_Click);
         this.MoveDn.Click += new EventHandler(this.MoveDn_Click);
         this.MoveBottom.Click += new EventHandler(this.MoveBottom_Click);
         this.FormItemLocaleSelector.UpdateRequested += new EventHandler(this.FormItemLocaleSelector_UpdateRequested);
         this.FormItemLocaleSelector.SelectedIndexChanged += new EventHandler(this.FormItemLocaleSelector_SelectedIndexChanged);
         this.FormItemLocaleSelector.Load += new EventHandler(this.FormItemLocaleSelector_Load);
      }

      protected void Page_Load(object sender, EventArgs e)
      {
         try
         {
            if (Request.Params["FormItemID"] != null)
               this.FormItem = Controller.Get(Int32.Parse(Request.Params["FormItemID"]));
            else
            {
               this.FormItem = new FormItemInfo();
               this.MovePanel.Visible = false;
               this.DeleteButton.Visible = false;
               this.FormItem.ModuleID = base.ModuleId;
            }

            if (!(Page.IsPostBack))
            {
               CreateFormTypeEntries();
               CreateLinkTargets();

               ImageButton deleteImage = (ImageButton)this.DeleteButton.Controls[0];
               LinkButton deleteLink = (LinkButton)this.DeleteButton.Controls[2];
               deleteImage.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem.Text") + "');");
               deleteLink.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem.Text") + "');");
               if (this.FormItem.FormItemID > 0)
               {
                  if ((this.FormItem.PortalID == base.PortalId) && (this.FormItem.ModuleID == base.ModuleId))
                  {
                     this.FormItemTypeList.SelectedValue = ((int)this.FormItem.FormItemType).ToString();

                     // Don't allow deletion of controls that are used for grouping, displayed in the submissions DropDownList,
                     // used as mail sender address or rendered as headline
                     if ((this.AddToDropDownListItem == String.Format("[FORMITEM]{0}", this.FormItem.FormItemID)) || (this.GroupOutputByItem == String.Format("[FORMITEM]{0}", this.FormItem.FormItemID)) || (this.RenderAsHeadlineItem == String.Format("[FORMITEM]{0}", this.FormItem.FormItemID)))
                     {
                        this.DeleteButton.Enabled = false;
                     }
                     else
                     {
                        if (this.SenderAddress == String.Format("[FORMITEM]{0}", this.FormItem.FormItemID))
                        {
                           this.RequiredCheckBox.Enabled = false;
                           this.DeleteButton.Enabled = false;
                        }
                        else
                        {
                           this.DeleteButton.Enabled = true;
                        }
                     }
                     this.FormItemWidthTextBox.Text = this.FormItem.Width.ToString();
                     this.FormItemHeightTextBox.Text = this.FormItem.Height.ToString();
                     this.FormItemRepeatColumnsTextBox.Text = this.FormItem.FormItemRepeatColumns.ToString();
                     this.FormItemOtherCheckBox.Checked = this.FormItem.FormItemOther;
                     this.FormItemLinkTargetDropDownList.SelectedValue = ((int)this.FormItem.FormItemLinkTarget).ToString();
                     this.ValidationExpressionTextBox.Text = this.FormItem.ValidationExpression;
                     if (this.FormItem.FormItemLabel == this.SenderAddress)
                        this.RequiredCheckBox.Checked = true;
                     else
                        this.RequiredCheckBox.Checked = this.FormItem.FormItemRequired;

                     this.MoveTop.Enabled = (this.FormItemIndex > 0);
                     this.MoveUp.Enabled = (this.FormItemIndex > 0);
                     this.MoveDn.Enabled = (this.FormItemIndex < (this.FormItems.Count - 1));
                     this.MoveBottom.Enabled = (this.FormItemIndex < (this.FormItems.Count - 1));
                  }
                  else
                  {
                     // Security violation attempt to access item not related to this module and/or portal
                     Response.Redirect(Globals.NavigateURL(), false);
                  }
               }
               Change_Type();
            }
            if ((this.HasSendToItem) && (this.FormItem.FormItemType != FormItemType.SendTo))
               this.FormItemTypeList.Items[Convert.ToInt32(FormItemType.SendTo) - 1].Enabled = false;
            if ((this.HideSendCopy) && (this.FormItem.FormItemType != FormItemType.SendCopy))
               this.FormItemTypeList.Items[Convert.ToInt32(FormItemType.SendCopy) - 1].Enabled = false;
            Get_Texts();
         }
         catch (Exception ex)
         {
            Exceptions.ProcessModuleLoadException(this, ex);
         }
      }

      protected void FormItemTypeList_SelectedIndexChanged(object sender, EventArgs e)
      {
         Change_Type();
      }

      protected void FormItemLocaleSelector_Load(object sender, EventArgs e)
      {
         LocaleSelector formItemLocaleSelector = (LocaleSelector)sender;
         if (!(Page.IsPostBack))
         {
            List<BabelFishInfo> aquarium = this.BabelFishController.GetStrings(base.PortalId, "helferlein.Form.FormItem." + this.FormItem.FormItemID.ToString(), false);
            if (aquarium != null)
            {
               foreach (BabelFishInfo fish in aquarium)
               {
                  this.Aquarium.Add(fish.StringKey + "." + fish.Locale, fish.StringText);
                  if (fish.Locale == this.FormItemLocaleSelector.CurrentLocale)
                  {
                     switch (fish.StringKey)
                     {
                        case "FormItemLabel":
                           this.FormItemLabelTextBox.Text = Server.HtmlDecode(fish.StringText);
                           break;
                        case "FormItemHelpText":
                           this.FormItemHelpTextTextBox.Text = fish.StringText;
                           break;
                        case "FormItemValue":
                           if (!(string.IsNullOrEmpty(fish.StringText)))
                           {
                              switch (this.FormItem.FormItemType)
                              {
                                 case FormItemType.Date:
                                    try
                                    {
                                       this.FormItemValuesTextBox.Text = DateTime.FromBinary(Convert.ToInt64(fish.StringText)).ToString("d");
                                    }
                                    catch
                                    {
                                       this.FormItemValuesTextBox.Text = fish.StringText;
                                    }
                                    break;
                                 default:
                                    this.FormItemValuesTextBox.Text = Server.HtmlDecode(fish.StringText);
                                    break;
                              }
                           }
                           break;
                        case "FormItemSelectedValue":
                           this.FormItemSelectedValuesTextBox.Text = fish.StringText;
                           break;
                        case "FormItemTemplate":
                           this.FormItemTemplateTextBox.Text = Server.HtmlDecode(fish.StringText);
                           break;
                        default:
                           break;
                     }
                  }
               }
            }
            this.FormItemLabelValidator.Enabled = ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0));
            this.FormItemLabelRequired.Visible = ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0));
         }
      }

      protected void FormItemLocaleSelector_UpdateRequested(object sender, EventArgs e)
      {
         LocaleSelector localeSelector = (LocaleSelector)sender;
         this.UpdateAquarium(localeSelector.ProcessedLocale);
      }

      protected void FormItemLocaleSelector_SelectedIndexChanged(object sender, EventArgs e)
      {
         LocaleSelector localeSelector = (LocaleSelector)sender;
         string s = String.Empty;
         if (this.FormItemLabelPanel.Visible)
         {
            this.FormItemLabelTextBox.Text = ((this.Aquarium.TryGetValue("FormItemLabel." + localeSelector.CurrentLocale, out s)) ? s : String.Empty);
            this.FormItemLabelValidator.Enabled = (localeSelector.CurrentLocale == PortalSettings.DefaultLanguage);
            this.FormItemLabelRequired.Visible = (localeSelector.CurrentLocale == PortalSettings.DefaultLanguage);
            this.FormItemHelpTextTextBox.Text = ((this.Aquarium.TryGetValue("FormItemHelpText." + localeSelector.CurrentLocale, out s)) ? s : String.Empty);
         }

         if (this.FormItemValuesPanel.Visible)
         {
            if (this.FormItem.FormItemType == FormItemType.Label)
               this.FormItemValuesTextBox.Text = ((this.Aquarium.TryGetValue("FormItemValue." + localeSelector.CurrentLocale, out s)) ? Server.HtmlDecode(s) : String.Empty);
            else
               this.FormItemValuesTextBox.Text = ((this.Aquarium.TryGetValue("FormItemValue." + localeSelector.CurrentLocale, out s)) ? s : String.Empty);

            FormItemType fit = ((FormItemType)Enum.GetValues(typeof(FormItemType)).GetValue(Convert.ToInt32(this.FormItemTypeList.SelectedValue) - 1));
            if (fit != FormItemType.Date)
            {
               this.FormItemValueValidator.Enabled = ((Helpers.ValueRequired(fit)) && ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0)));
               this.FormItemValueRequired.Visible = ((Helpers.ValueRequired(fit)) && ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0)));
               this.FormItemValueDateValidator.Enabled = false;
               this.FormItemValueDateValidator.Visible = false;
            }
            else
            {
               this.FormItemValueValidator.Enabled = false;
               this.FormItemValueRequired.Visible = false;
               this.FormItemValueDateValidator.Enabled = true;
               this.FormItemValueDateValidator.Visible = true;
            }
         }

         if (this.FormItemSelectedValuesPanel.Visible)
            this.FormItemSelectedValuesTextBox.Text = ((this.Aquarium.TryGetValue("FormItemSelectedValue." + localeSelector.CurrentLocale, out s)) ? s : String.Empty);

         if (this.Aquarium.TryGetValue("FormItemTemplate." + localeSelector.CurrentLocale, out s))
            this.FormItemTemplateTextBox.Text = Server.HtmlDecode(s);
         else
            this.FormItemTemplateTextBox.Text = String.Empty;
      }

      protected void ResetFormItemTemplateHyperLink_Load(object sender, EventArgs e)
      {
         HyperLink resetFormItemTemplateHyperLink = (HyperLink)sender;
         if (!(base.Page.ClientScript.IsClientScriptBlockRegistered("ResetFormItemTemplate")))
         {
            StringBuilder scriptBuilder = new StringBuilder();
            scriptBuilder.Append("function ResetFormItemTemplate_ClientClick() {");
            scriptBuilder.Append("var itemTemplateTextBox = document.getElementById(\"");
            scriptBuilder.Append(this.FormItemTemplateTextBox.ClientID);
            scriptBuilder.Append("\");");
            scriptBuilder.Append("itemTemplateTextBox.value = \"");
            scriptBuilder.Append(Server.HtmlDecode(this.FormItem.FormItemTemplate).Replace("\"", "\\\"").Replace("\n", "\\n").Replace("\r", "\\r"));
            scriptBuilder.Append("\";");
            scriptBuilder.Append("}");
            base.Page.ClientScript.RegisterClientScriptBlock(base.Page.GetType(), "ResetFormItemTemplate", scriptBuilder.ToString(), true);
         }
         resetFormItemTemplateHyperLink.NavigateUrl = "javascript:ResetFormItemTemplate_ClientClick();";
      }

      protected void SetDefaultFormItemTemplateHyperLink_Load(object sender, EventArgs e)
      {
         HyperLink setDefaultFormItemTemplateHyperLink = (HyperLink)sender;
         if (!(base.Page.ClientScript.IsClientScriptBlockRegistered("SetDefaultFormItemTemplate")))
         {
            StringBuilder scriptBuilder = new StringBuilder();
            scriptBuilder.Append("function SetDefaultFormItemTemplate_ClientClick() {");
            scriptBuilder.Append("var itemTemplateTextBox = document.getElementById(\"");
            scriptBuilder.Append(this.FormItemTemplateTextBox.ClientID);
            scriptBuilder.Append("\");");
            scriptBuilder.Append("itemTemplateTextBox.value = \"");
            scriptBuilder.Append(this.FormItemTemplate.Replace("\"", "\\\"").Replace("\n", "\\n").Replace("\r", "\\r"));
            scriptBuilder.Append("\";");
            scriptBuilder.Append("}");
            base.Page.ClientScript.RegisterClientScriptBlock(base.Page.GetType(), "SetDefaultFormItemTemplate", scriptBuilder.ToString(), true);
         }
         setDefaultFormItemTemplateHyperLink.NavigateUrl = "javascript:SetDefaultFormItemTemplate_ClientClick();";
      }

      protected void FormItemOtherCheckBox_CheckedChanged(object sender, EventArgs e)
      {
         CheckBox formItemOtherCheckBox = (CheckBox)sender;
         this.FormItemWidthPanel.Visible = formItemOtherCheckBox.Checked;
         this.FormItemHeightPanel.Visible = formItemOtherCheckBox.Checked;
      }

      protected void UpdateButton_Click(object sender, EventArgs e)
      {
         if (Page.IsValid)
         {
            try
            {
               this.FormItem.Locale = this.FormItemLocaleSelector.CurrentLocale;
               this.UpdateAquarium(this.FormItemLocaleSelector.CurrentLocale);

               string s;
               bool allRequiredValuesOK = true;
               if (this.FormItemLabelPanel.Visible)
               {
                  bool labelOK = this.Aquarium.TryGetValue("FormItemLabel." + base.PortalSettings.DefaultLanguage, out s);
                  if (((labelOK) && (String.IsNullOrEmpty(s))) || (!(labelOK)))
                  {
                     this.NoLabelForPortalDefaultErrorMessageLabel.Text = Localization.GetString("NoLabelForPortalDefaultErrorMessageLabel.Text", LocalResourceFile);
                     this.NoLabelForPortalDefaultErrorMessageLabel.Visible = true;
                     allRequiredValuesOK = false;
                  }
               }
               if ((this.FormItemValuesPanel.Visible) && (Helpers.ValueRequired(this.FormItem.FormItemType)))
               {
                  bool valueOK = this.Aquarium.TryGetValue("FormItemValue." + base.PortalSettings.DefaultLanguage, out s);
                  if (((valueOK) && (String.IsNullOrEmpty(s))) || (!(valueOK)))
                  {
                     this.NoValueForPortalDefaultErrorMessageLabel.Text = Localization.GetString("NoValueForPortalDefaultErrorMessageLabel.Text", LocalResourceFile);
                     this.NoValueForPortalDefaultErrorMessageLabel.Visible = true;
                     allRequiredValuesOK = false;
                  }
               }
               if (allRequiredValuesOK)
               {
                  this.UpdateValues();
                  Response.Redirect(Globals.NavigateURL(), false);
               }
            }
            catch (Exception ex)
            {
               Exceptions.ProcessModuleLoadException(this, ex);
            }
         }
      }

      protected void CancelButton_Click(object sender, EventArgs e)
      {
         try
         {
            Response.Redirect(Globals.NavigateURL(), false);
         }
         catch (Exception ex)
         {
            Exceptions.ProcessModuleLoadException(this, ex);
         }
      }

      protected void DeleteButton_Click(object sender, EventArgs e)
      {
         try
         {
            if (this.FormItem.FormItemID > 0)
               Controller.Drop(this.FormItem.FormItemID);
            Response.Redirect(Globals.NavigateURL(), false);
         }
         catch (Exception ex)
         {
            Exceptions.ProcessModuleLoadException(this, ex);
         }
      }

      protected void MoveTop_Click(object sender, EventArgs e)
      {
         if (this.FormItemIndex > 0)
         {
            this.Controller.MoveTop(this.FormItem.FormItemID);
         }
         Response.Redirect(Globals.NavigateURL(), false);
      }

      protected void MoveUp_Click(object sender, EventArgs e)
      {
         if (this.FormItemIndex > 0)
         {
            this.Controller.Move(this.FormItem.FormItemID, -1);
         }
         Response.Redirect(Globals.NavigateURL(), false);
      }

      protected void MoveDn_Click(object sender, EventArgs e)
      {
         if (this.FormItemIndex < this.FormItems.Count - 1)
         {
            this.Controller.Move(this.FormItem.FormItemID, 1);
         }
         Response.Redirect(Globals.NavigateURL(), false);
      }

      protected void MoveBottom_Click(object sender, EventArgs e)
      {
         if (this.FormItemIndex < this.FormItems.Count - 1)
         {
            this.Controller.MoveBottom(this.FormItem.FormItemID);
         }
         Response.Redirect(Globals.NavigateURL(), false);
      }
#endregion

#region Private Methods
      protected void Get_Texts()
      {
         this.FormItemLabelValidator.ErrorMessage = Localization.GetString("FormItemLabelValidator.ErrorMessage", base.LocalResourceFile);
         this.FormItemLabelRequired.AlternateText = Localization.GetString("FormItemLabelRequired.AlternateText", base.LocalResourceFile);
         this.FormItemValueValidator.ErrorMessage = Localization.GetString("FormItemValueValidator.ErrorMessage", base.LocalResourceFile);
         this.FormItemValueDateValidator.InvalidErrorMessage = Localization.GetString("FormItemValueDateValidator.InvalidErrorMessage", base.LocalResourceFile);
         this.FormItemValueDateValidator.RequiredErrorMessage = Localization.GetString("FormItemValueDateValidator.RequiredErrorMessage", base.LocalResourceFile);
         this.FormItemValueRequired.AlternateText = Localization.GetString("FormItemValueRequired.AlternateText", base.LocalResourceFile);
         this.FormItemWidthTextBoxValidator.ErrorMessage = Localization.GetString("FormItemWidthTextBoxValidator.ErrorMessage", base.LocalResourceFile);
         this.FormItemHeightTextBoxValidator.ErrorMessage = Localization.GetString("FormItemHeightTextBoxValidator.ErrorMessage", base.LocalResourceFile);
      }

      protected void Change_Type()
      {
         // Hide all panels...
         this.FormItemLabelPanel.Visible = false;
         this.FormItemHelpTextPanel.Visible = false;
         this.FormItemValuesPanel.Visible = false;
         this.FormItemValueRequired.Visible = false;
         this.FormItemValueValidator.Visible = false;
         this.FormItemValueValidator.Enabled = false;
         this.FormItemValueDateValidator.Visible = false;
         this.FormItemValueDateValidator.Enabled = false;
         this.FormItemSelectedValuesPanel.Visible = false;
         this.FormItemWidthPanel.Visible = false;
         this.FormItemHeightPanel.Visible = false;
         this.FormItemRepeatColumnsPanel.Visible = false;
         this.RequiredPanel.Visible = false;
         this.FormItemLinkTargetPanel.Visible = false;
         this.ValidationExpressionPanel.Visible = false;
         this.FormItemOtherPanel.Visible = false;

         this.FormItemValuesLabel.ResourceKey = "FormItemValuesLabel";

         FormItemType fit = ((FormItemType)Enum.GetValues(typeof(FormItemType)).GetValue(Convert.ToInt32(this.FormItemTypeList.SelectedValue) - 1));
         switch (fit)
         {
            case FormItemType.TextBox:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.SingleLine;
               this.FormItemWidthPanel.Visible = true;
               this.RequiredPanel.Visible = true;
               this.ValidationExpressionPanel.Visible = true;
               break;
            case FormItemType.TextArea:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.MultiLine;
               this.FormItemValuesTextBox.Height = Unit.Parse("150px");
               this.FormItemWidthPanel.Visible = true;
               this.RequiredPanel.Visible = true;
               this.FormItemHeightPanel.Visible = true;
               break;
            case FormItemType.DropDownList:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.MultiLine;
               this.FormItemValuesTextBox.Height = Unit.Parse("150px");
               this.FormItemSelectedValuesPanel.Visible = true;
               this.FormItemOtherPanel.Visible = true;
               this.FormItemWidthPanel.Visible = (this.FormItemOtherCheckBox.Checked);
               this.FormItemHeightPanel.Visible = (this.FormItemOtherCheckBox.Checked);
               break;
            case FormItemType.MultipleSelect:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.MultiLine;
               this.FormItemValuesTextBox.Height = Unit.Parse("150px");
               this.FormItemSelectedValuesPanel.Visible = true;
               this.RequiredPanel.Visible = true;
               this.FormItemRepeatColumnsPanel.Visible = true;
               this.FormItemOtherPanel.Visible = true;
               this.FormItemWidthPanel.Visible = (this.FormItemOtherCheckBox.Checked);
               this.FormItemHeightPanel.Visible = (this.FormItemOtherCheckBox.Checked);
               this.FormItemSelectedValuesTextBox.TextMode = TextBoxMode.MultiLine;
               this.FormItemSelectedValuesTextBox.Height = Unit.Parse("75px");
               break;
            case FormItemType.Checkbox:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.SingleLine;
               this.RequiredPanel.Visible = true;
               break;
            case FormItemType.Label:
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.MultiLine;
               this.FormItemValuesTextBox.Height = Unit.Parse("150px");
               break;
            case FormItemType.RadioButtonList:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.MultiLine;
               this.FormItemValuesTextBox.Height = Unit.Parse("150px");
               this.FormItemSelectedValuesPanel.Visible = true;
               this.RequiredPanel.Visible = true;
               this.FormItemRepeatColumnsPanel.Visible = true;
               this.FormItemOtherPanel.Visible = true;
               this.FormItemWidthPanel.Visible = (this.FormItemOtherCheckBox.Checked);
               this.FormItemHeightPanel.Visible = (this.FormItemOtherCheckBox.Checked);
               break;
            case FormItemType.EMailAddress:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemWidthPanel.Visible = true;
               this.RequiredPanel.Visible = true;
               this.ValidationExpressionPanel.Visible = true;
               break;
            case FormItemType.WebSite:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemWidthPanel.Visible = true;
               this.FormItemLinkTargetPanel.Visible = true;
               this.RequiredPanel.Visible = true;
               this.ValidationExpressionPanel.Visible = true;
               break;
            case FormItemType.Date:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.SingleLine;
               this.RequiredPanel.Visible = true;
               break;
            case FormItemType.SendTo:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemSelectedValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.MultiLine;
               this.FormItemValuesLabel.ResourceKey = "FormItemSendToValuesLabel";
               break;
            case FormItemType.FileUpload:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.RequiredPanel.Visible = true;
               break;
            case FormItemType.SendCopy:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemHelpTextPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               break;
            case FormItemType.Hidden:
               this.FormItemLabelPanel.Visible = true;
               this.FormItemValuesPanel.Visible = true;
               this.FormItemValuesTextBox.TextMode = TextBoxMode.SingleLine;
               break;
            default:
               break;
         }
         this.CommonPropertiesPanel.Visible = ((this.FormItemWidthPanel.Visible) || (this.FormItemHeightPanel.Visible) || (this.FormItemRepeatColumnsPanel.Visible) || (this.RequiredPanel.Visible) || (this.FormItemLinkTargetPanel.Visible) || (this.ValidationExpressionPanel.Visible) || (this.FormItemOtherPanel.Visible));
         this.FormItemValueValidator.Enabled = ((Helpers.ValueRequired(fit)) || ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0)));

         this.FormItemLabelValidator.Enabled = this.FormItemLabelPanel.Visible;

         if (fit != FormItemType.Date)
         {
            this.FormItemValueValidator.Enabled = ((Helpers.ValueRequired(fit)) && ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0)));
            this.FormItemValueValidator.Visible = ((Helpers.ValueRequired(fit)) && ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0)));
            this.FormItemValueRequired.Visible = ((Helpers.ValueRequired(fit)) && ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0)));
            this.FormItemValueRequired.Visible = ((Helpers.ValueRequired(fit)) && ((this.FormItemLocaleSelector.CurrentLocale == PortalSettings.DefaultLanguage) || (this.FormItemLocaleSelector.Count == 0)));
            this.FormItemValueDateValidator.Enabled = false;
            this.FormItemValueDateValidator.Visible = false;
         }
         else
         {
            this.FormItemValueValidator.Enabled = false;
            this.FormItemValueValidator.Visible = false;
            this.FormItemValueRequired.Visible = false;
            this.FormItemValueRequired.Visible = false;
            this.FormItemValueDateValidator.Enabled = true;
            this.FormItemValueDateValidator.Visible = true;
         }

      }

      protected void CreateFormTypeEntries()
      {
         string[] formTypeNames = Enum.GetNames(typeof(FormItemType));
         Dictionary<int, string> formTypeEntries = new Dictionary<int, string>();
         for (int i = 0; i < formTypeNames.Length; i++)
            formTypeEntries.Add(Convert.ToInt32(Enum.GetValues(typeof(FormItemType)).GetValue(i)), Localization.GetString("FormItemType." + formTypeNames[i], this.SharedResourceFile));
         this.FormItemTypeList.DataSource = formTypeEntries;
         this.FormItemTypeList.DataTextField = "value";
         this.FormItemTypeList.DataValueField = "key";
         this.FormItemTypeList.DataBind();
      }

      protected void CreateLinkTargets()
      {
         string[] linkTargetNames = Enum.GetNames(typeof(LinkTarget));
         Dictionary<int, string> linkTargetEntries = new Dictionary<int, string>();
         for (int i = 0; i < linkTargetNames.Length; i++)
            linkTargetEntries.Add(Convert.ToInt32(Enum.GetValues(typeof(LinkTarget)).GetValue(i)), Localization.GetString("LinkTarget." + linkTargetNames[i], this.SharedResourceFile));
         this.FormItemLinkTargetDropDownList.DataSource = linkTargetEntries;
         this.FormItemLinkTargetDropDownList.DataTextField = "value";
         this.FormItemLinkTargetDropDownList.DataValueField = "key";
         this.FormItemLinkTargetDropDownList.DataBind();
      }

      protected void UpdateValues()
      {
         this.FormItem.FormItemType = (FormItemType)Enum.GetValues(typeof(FormItemType)).GetValue(Convert.ToInt32(this.FormItemTypeList.SelectedValue) - 1);

         if ((this.FormItemWidthPanel.Visible) && (!(String.IsNullOrEmpty(this.FormItemWidthTextBox.Text))))
         {
            this.FormItem.Width = Convert.ToInt32(this.FormItemWidthTextBox.Text);
         }
         else
         {
            this.FormItem.Width = null;
         }

         if ((this.FormItemHeightPanel.Visible) && (!(String.IsNullOrEmpty(this.FormItemHeightTextBox.Text))))
            this.FormItem.Height = Convert.ToInt32(this.FormItemHeightTextBox.Text);
         else
            this.FormItem.Height = null;

         if ((this.FormItemRepeatColumnsPanel.Visible) && (!(String.IsNullOrEmpty(this.FormItemRepeatColumnsTextBox.Text))))
            this.FormItem.FormItemRepeatColumns = Convert.ToInt32(this.FormItemRepeatColumnsTextBox.Text);
         else
            this.FormItem.FormItemRepeatColumns = null;

         if (this.RequiredPanel.Visible)
         {
            this.FormItem.FormItemRequired = this.RequiredCheckBox.Checked;
         }

         // <!-- Code provided by user Lode_Leroy on CodePlex
         if (this.FormItemOtherPanel.Visible)
         {
            this.FormItem.FormItemOther = this.FormItemOtherCheckBox.Checked;
         }
         // Code provided by user Lode_Leroy on CodePlex -->

         if (this.FormItemLinkTargetPanel.Visible)
         {
            this.FormItem.FormItemLinkTarget = (LinkTarget)Enum.GetValues(typeof(LinkTarget)).GetValue(Convert.ToInt32(this.FormItemLinkTargetDropDownList.SelectedValue) - 1);
         }

         if (this.ValidationExpressionPanel.Visible)
            this.FormItem.ValidationExpression = this.ValidationExpressionTextBox.Text;
         else
            this.FormItem.ValidationExpression = String.Empty;

         if (this.FormItem.FormItemID == 0)
         {
            Controller.Add(this.FormItem, this.Aquarium);
         }
         else
         {
            Controller.Change(this.FormItem, this.Aquarium);
         }
      }

      protected void UpdateAquarium(string locale)
      {
         string s;
         if (this.FormItemLabelPanel.Visible)
         {
            if (this.Aquarium.TryGetValue("FormItemLabel." + locale, out s))
               this.Aquarium["FormItemLabel." + locale] = HtmlUtils.StripTags(this.FormItemLabelTextBox.Text, false).Trim();
            else
               this.Aquarium.Add("FormItemLabel." + locale, HtmlUtils.StripTags(this.FormItemLabelTextBox.Text, false).Trim());

            if (this.Aquarium.TryGetValue("FormItemHelpText." + locale, out s))
               this.Aquarium["FormItemHelpText." + locale] = HtmlUtils.StripTags(this.FormItemHelpTextTextBox.Text, false).Trim();
            else
               this.Aquarium.Add("FormItemHelpText." + locale, HtmlUtils.StripTags(this.FormItemHelpTextTextBox.Text, false).Trim());
         }

         if (this.FormItemValuesPanel.Visible)
         {
            if (this.Aquarium.TryGetValue("FormItemValue." + locale, out s))
            {
               // HtmlTags are allowed in the Label control only
               switch (this.formItem.FormItemType)
               {
                  case FormItemType.Label:
                     this.Aquarium["FormItemValue." + locale] = this.PortalSecurity.InputFilter(this.FormItemValuesTextBox.Text, PortalSecurity.FilterFlag.NoMarkup).Trim();
                     break;
                  case FormItemType.Date:
                     if (String.IsNullOrEmpty(this.FormItemValuesTextBox.Text))
                     {
                        this.Aquarium["FormItemValue." + locale] = String.Empty;
                     }
                     else
                     {
                        try { this.Aquarium["FormItemValue." + locale] = DateTime.Parse(this.FormItemValuesTextBox.Text).ToBinary().ToString(); }
                        catch (Exception ex) { Exceptions.LogException(ex); }
                     }
                     break;
                  default:
                     this.Aquarium["FormItemValue." + locale] = HtmlUtils.StripTags(this.FormItemValuesTextBox.Text, false).Trim();
                     break;
               }
            }
            else
            {
               // HtmlTags are allowed in the Label control only
               switch (this.formItem.FormItemType)
               {
                  case FormItemType.Label:
                     this.Aquarium.Add("FormItemValue." + locale, this.PortalSecurity.InputFilter(this.FormItemValuesTextBox.Text.Trim(), PortalSecurity.FilterFlag.NoMarkup));
                     break;
                  case FormItemType.Date:
                     if (String.IsNullOrEmpty(this.FormItemValuesTextBox.Text))
                     {
                        this.Aquarium["FormItemValue." + locale] = String.Empty;
                     }
                     else
                     {
                        try { this.Aquarium.Add("FormItemValue." + locale, DateTime.Parse(this.FormItemValuesTextBox.Text).ToBinary().ToString()); }
                        catch (Exception ex) { Exceptions.LogException(ex); }
                     }
                     break;
                  default:
                     this.Aquarium.Add("FormItemValue." + locale, HtmlUtils.StripTags(this.FormItemValuesTextBox.Text, false).Trim());
                     break;
               }
            }
         }

         if (this.FormItemSelectedValuesPanel.Visible)
         {
            if (this.Aquarium.TryGetValue("FormItemSelectedValue." + locale, out s))
               this.Aquarium["FormItemSelectedValue." + locale] = HtmlUtils.StripTags(this.FormItemSelectedValuesTextBox.Text, false).Trim();
            else
               this.Aquarium.Add("FormItemSelectedValue." + locale, HtmlUtils.StripTags(this.FormItemSelectedValuesTextBox.Text, false).Trim());
         }

         if (this.Aquarium.TryGetValue("FormItemTemplate." + locale, out s))
            this.Aquarium["FormItemTemplate." + locale] = this.PortalSecurity.InputFilter(this.FormItemTemplateTextBox.Text, PortalSecurity.FilterFlag.NoMarkup).Trim();
         else
            this.Aquarium.Add("FormItemTemplate." + locale, this.PortalSecurity.InputFilter(this.FormItemTemplateTextBox.Text, PortalSecurity.FilterFlag.NoMarkup).Trim());
      }
#endregion
   }
}